package com.bishe.app.modules.role.controller;

import com.bishe.app.core.api.CommonResult;
import com.bishe.app.core.api.CommonPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bishe.app.core.enums.IsDel;
import com.bishe.app.core.utils.Lock;
import com.bishe.app.modules.menu.entity.Menu;
import com.bishe.app.modules.menu.entity.MenuTree;
import com.bishe.app.modules.menu.service.MenuService;
import com.bishe.app.modules.role.model.RoleSearch;
import com.bishe.app.modules.rolemenu.entity.RoleMenu;
import com.bishe.app.modules.rolemenu.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bishe.app.modules.role.service.RoleService;
import com.bishe.app.modules.role.entity.Role;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author
 * @since 2024-03-13
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    public RoleService roleService;

    @Resource
    private RoleMenuService roleMenuService;

    @Resource
    private MenuService menuService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    @Lock(lockPrefix = "/role",key = "lock", lockSecond = 60)
    public CommonResult create(@RequestBody Role role) {
        boolean success = roleService.save(role);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @ResponseBody
    @Lock(lockPrefix = "/role",key = "lock", lockSecond = 60)
    public CommonResult create(Long roleId) {
        Role byId = roleService.getById(roleId);

        if (Objects.isNull(byId)) {
            return CommonResult.success(null);
        }

        Integer isDel = byId.getIsDel();
        if (isDel == 0) {
            roleService.lambdaUpdate()
                    .eq(Role::getId, roleId)
                    .set(Role::getIsDel, IsDel.Y.getCode())
                    .update();
        } else {
            roleService.lambdaUpdate()
                    .eq(Role::getId, roleId)
                    .set(Role::getIsDel, IsDel.N.getCode())
                    .update();
        }

        return CommonResult.success(null);


    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    @Lock(lockPrefix = "/role",key = "lock", lockSecond = 60)
    public CommonResult update(@PathVariable Long id, @RequestBody Role role) {
        role.setId(id);
        boolean success = roleService.updateById(role);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    @Lock(lockPrefix = "/role",key = "lock", lockSecond = 60)
    public CommonResult delete(@PathVariable Long id) {
        boolean success = roleService.removeById(id);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @Lock(lockPrefix = "/role",key = "lock", lockSecond = 60)
    public CommonResult deleteBatch(@RequestParam("ids") List<Long> ids) {
        boolean success = roleService.removeByIds(ids);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }


    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Role>> listAll() {
        List<Role> roleList = roleService.list();
        return CommonResult.success(roleList);
    }

    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<Role>> page(@RequestBody CommonPage commonPage) {
        Page<Role> page = new Page(commonPage.getPageNum(), commonPage.getPageSize());
        return CommonResult.success(CommonPage.restPage(roleService.page(page)));
    }


    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<Role>> search(@RequestBody RoleSearch search) {
        Page<Role> page = new Page(search.getPageNum(), search.getPageSize());

        if (Objects.nonNull(search.getName())) {
            Page<Role> result = roleService.lambdaQuery()
                    .like(Role::getName, search.getName())
                    .page(page);
            return CommonResult.success(CommonPage.restPage(result));
        }
        return CommonResult.success(CommonPage.restPage(roleService.page(page)));
    }


    @RequestMapping(value = "/getRoleMenuIdList/{roleId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Long>> getRoleMenuId(@PathVariable Long roleId) {
        List<RoleMenu> roleMenuList = roleMenuService.lambdaQuery()
                .eq(RoleMenu::getRoleId, roleId)
                .eq(RoleMenu::getIsDel, IsDel.N.getCode())
                .list();

        if (CollectionUtils.isEmpty(roleMenuList)) {
            return CommonResult.success(new ArrayList<>());
        }

        List<Long> menuIds = roleMenuList.stream().map(x -> x.getMenuId()).collect(Collectors.toList());
        List<Menu> menus = menuService.listByIds(menuIds);
        // 遍历到跟
         return CommonResult.success(MenuTree.getLeafIds(menus));
    }

}



